package com.swinw.asumall.member.service.impl;

import com.swinw.common.utils.R;
import com.swinw.common.vo.MemberVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.Query;

import com.swinw.asumall.member.dao.MemberDao;
import com.swinw.asumall.member.entity.MemberEntity;
import com.swinw.asumall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R userLogin(MemberEntity memberEntity) {
        String username = memberEntity.getUsername();
        String password = memberEntity.getPassword();
        if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)){
            QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",username).or().eq("mobile",username);
            MemberEntity entity = this.baseMapper.selectOne(queryWrapper);
            //密码验证
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            if(passwordEncoder.matches(password,entity.getPassword())){
                MemberVo memberVo = new MemberVo();
                memberVo.setId(entity.getId());
                memberVo.setBirth(entity.getBirth());
                memberVo.setCity(entity.getCity());
                memberVo.setCreateTime(entity.getCreateTime());
                memberVo.setEmail(entity.getEmail());
                memberVo.setGender(entity.getGender());
                memberVo.setGrowth(entity.getGrowth());
                memberVo.setHeader(entity.getHeader());
                memberVo.setIntegration(entity.getIntegration());
                memberVo.setJob(entity.getJob());
                memberVo.setLevelId(entity.getLevelId());
                memberVo.setMobile(entity.getMobile());
                memberVo.setNickname(entity.getNickname());
                memberVo.setPassword(entity.getPassword());
                memberEntity.setUsername(entity.getUsername());
                return R.ok().put("user",memberVo);
            }else{
                return R.error(-1,"帐号或密码不正确");
            }
        }else{
            return R.error(-1,"帐号或密码不正确");
        }
    }

    /**
     * 用户保存操作
     * 1、判断电话号码和用户名是否重复
     * 2、执行保存
     * @param entity
     * @return
     */
    @Override
    public boolean save(MemberEntity entity) {
        //1、判断数据库中是否存在当前的电话号码和用户名
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",entity.getUsername()).or().eq("mobile",entity.getMobile());
        List<MemberEntity> memberEntities = this.baseMapper.selectList(queryWrapper);
        if(memberEntities!=null && memberEntities.size()>0){
            return false;
        }else{
            entity.setCreateTime(new Date());
            entity.setLevelId(1L);
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            entity.setPassword(encoder.encode(entity.getPassword()));
            return super.save(entity);
        }
    }
}